﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;

namespace Moria
{
    public class BackgroundFactory
    {
        private class Background
        {
            public Background(string info, int roll, int chart, int next, int bonus)
            {
                this.info = info;
                this.roll = roll;
                this.chart = chart;
                this.next = next;
                this.bonus = bonus;
            }
            public string info;		// History information		
            public int roll;		// Die roll needed for history	
            public int chart;		// Table number			
            public int next;		// Pointer to next table	
            public int bonus;		// Bonus to the Social Class+50	
        }


        private readonly Background[] chart = new Background[] 
        { 
            #region chart
            new Background("You are the illegitimate and unacknowledged child ",		 10, 1, 2, 25),
            new Background("You are the illegitimate but acknowledged child ",		 20, 1, 2, 35),
            new Background("You are one of several children ",				 95, 1, 2, 45),
            new Background("You are the first child ",					100, 1, 2, 50),
            new Background("of a Serf.  ",						 40, 2, 3, 65),
            new Background("of a Yeoman.  ",						 65, 2, 3, 80),
            new Background("of a Townsman.  ",						 80, 2, 3, 90),
            new Background("of a Guildsman.  ",						 90, 2, 3,105),
            new Background("of a Landed Knight.  ",					 96, 2, 3,120),
            new Background("of a Titled Noble.  ",					 99, 2, 3,130),
            new Background("of a Royal Blood Line.  ",					100, 2, 3,140),
            new Background("You are the black sheep of the family.  ",			 20, 3,50, 20),
            new Background("You are a credit to the family.  ",				 80, 3,50, 55),
            new Background("You are a well liked child.  ",				100, 3,50, 60),
            new Background("Your mother was a Green-Elf.  ",				 40, 4, 1, 50),
            new Background("Your father was a Green-Elf.  ",				 75, 4, 1, 55),
            new Background("Your mother was a Grey-Elf.  ",				 90, 4, 1, 55),
            new Background("Your father was a Grey-Elf.  ",				 95, 4, 1, 60),
            new Background("Your mother was a High-Elf.  ",				 98, 4, 1, 65),
            new Background("Your father was a High-Elf.  ",				100, 4, 1, 70),
            new Background("You are one of several children ",				 60, 7, 8, 50),
            new Background("You are the only child ",					100, 7, 8, 55),
            new Background("of a Green-Elf ",						 75, 8, 9, 50),
            new Background("of a Grey-Elf ",						 95, 8, 9, 55),
            new Background("of a High-Elf ",						100, 8, 9, 60),
            new Background("Ranger.  ",							 40, 9,54, 80),
            new Background("Archer.  ",							 70, 9,54, 90),
            new Background("Warrior.  ",							 87, 9,54,110),
            new Background("Mage.  ",							 95, 9,54,125),
            new Background("Prince.  ",							 99, 9,54,140),
            new Background("King.  ",							100, 9,54,145),
            new Background("You are one of several children of a Halfling ",		 85,10,11, 45),
            new Background("You are the only child of a Halfling ",			100,10,11, 55),
            new Background("Bum.  ",							 20,11, 3, 55),
            new Background("Tavern Owner.  ",						 30,11, 3, 80),
            new Background("Miller.  ",							 40,11, 3, 90),
            new Background("Home Owner.  ",						 50,11, 3,100),
            new Background("Burglar.  ",							 80,11, 3,110),
            new Background("Warrior.  ",							 95,11, 3,115),
            new Background("Mage.  ",							 99,11, 3,125),
            new Background("Clan Elder.  ",						100,11, 3,140),
            new Background("You are one of several children of a Gnome ",			 85,13,14, 45),
            new Background("You are the only child of a Gnome ",				100,13,14, 55),
            new Background("Beggar.  ",							 20,14, 3, 55),
            new Background("Braggart.  ",							 50,14, 3, 70),
            new Background("Prankster.  ",						 75,14, 3, 85),
            new Background("Warrior.  ",							 95,14, 3,100),
            new Background("Mage.  ",							100,14, 3,125),
            new Background("You are one of two children of a Dwarven ",			 25,16,17, 40),
            new Background("You are the only child of a Dwarven ",			100,16,17, 50),
            new Background("Thief.  ",							 10,17,18, 60),
            new Background("Prison Guard.  ",						 25,17,18, 75),
            new Background("Miner.  ",							 75,17,18, 90),
            new Background("Warrior.  ",							 90,17,18,110),
            new Background("Priest.  ",							 99,17,18,130),
            new Background("King.  ",							100,17,18,150),
            new Background("You are the black sheep of the family.  ",			 15,18,57, 10),
            new Background("You are a credit to the family.  ",				 85,18,57, 50),
            new Background("You are a well liked child.  ",				100,18,57, 55),
            new Background("Your mother was an Orc, but it is unacknowledged.  ",		 25,19,20, 25),
            new Background("Your father was an Orc, but it is unacknowledged.  ",		100,19,20, 25),
            new Background("You are the adopted child ",					100,20, 2, 50),
            new Background("Your mother was a Cave-Troll ",				 30,22,23, 20),
            new Background("Your father was a Cave-Troll ",				 60,22,23, 25),
            new Background("Your mother was a Hill-Troll ",				 75,22,23, 30),
            new Background("Your father was a Hill-Troll ",				 90,22,23, 35),
            new Background("Your mother was a Water-Troll ",				 95,22,23, 40),
            new Background("Your father was a Water-Troll ",				100,22,23, 45),
            new Background("Cook.  ",							  5,23,62, 60),
            new Background("Warrior.  ",							 95,23,62, 55),
            new Background("Shaman.  ",							 99,23,62, 65),
            new Background("Clan Chief.  ",						100,23,62, 80),
            new Background("You have dark brown eyes, ",					 20,50,51, 50),
            new Background("You have brown eyes, ",					 60,50,51, 50),
            new Background("You have hazel eyes, ",					 70,50,51, 50),
            new Background("You have green eyes, ",					 80,50,51, 50),
            new Background("You have blue eyes, ",					 90,50,51, 50),
            new Background("You have blue-gray eyes, ",					100,50,51, 50),
            new Background("straight ",							 70,51,52, 50),
            new Background("wavy ",							 90,51,52, 50),
            new Background("curly ",							100,51,52, 50),
            new Background("black hair, ",						 30,52,53, 50),
            new Background("brown hair, ",						 70,52,53, 50),
            new Background("auburn hair, ",						 80,52,53, 50),
            new Background("red hair, ",							 90,52,53, 50),
            new Background("blond hair, ",						100,52,53, 50),
            new Background("and a very dark complexion.",					 10,53, 0, 50),
            new Background("and a dark complexion.",					 30,53, 0, 50),
            new Background("and an average complexion.",					 80,53, 0, 50),
            new Background("and a fair complexion.",					 90,53, 0, 50),
            new Background("and a very fair complexion.",					100,53, 0, 50),
            new Background("You have light grey eyes, ",					 85,54,55, 50),
            new Background("You have light blue eyes, ",					 95,54,55, 50),
            new Background("You have light green eyes, ",					100,54,55, 50),
            new Background("straight ",							 75,55,56, 50),
            new Background("wavy ",							100,55,56, 50),
            new Background("black hair, and a fair complexion.",				 75,56, 0, 50),
            new Background("brown hair, and a fair complexion.",				 85,56, 0, 50),
            new Background("blond hair, and a fair complexion.",				 95,56, 0, 50),
            new Background("silver hair, and a fair complexion.",				100,56, 0, 50),
            new Background("You have dark brown eyes, ",					 99,57,58, 50),
            new Background("You have glowing red eyes, ",					100,57,58, 60),
            new Background("straight ",							 90,58,59, 50),
            new Background("wavy ",							100,58,59, 50),
            new Background("black hair, ",						 75,59,60, 50),
            new Background("brown hair, ",						100,59,60, 50),
            new Background("a one foot beard, ",						 25,60,61, 50),
            new Background("a two foot beard, ",						 60,60,61, 51),
            new Background("a three foot beard, ",					 90,60,61, 53),
            new Background("a four foot beard, ",						100,60,61, 55),
            new Background("and a dark complexion.",					100,61, 0, 50),
            new Background("You have slime green eyes, ",					 60,62,63, 50),
            new Background("You have puke yellow eyes, ",					 85,62,63, 50),
            new Background("You have blue-bloodshot eyes, ",				 99,62,63, 50),
            new Background("You have glowing red eyes, ",					100,62,63, 55),
            new Background("dirty ",							 33,63,64, 50),
            new Background("mangy ",							 66,63,64, 50),
            new Background("oily ",							100,63,64, 50),
            new Background("sea-weed green hair, ",					 33,64,65, 50),
            new Background("bright red hair, ",						 66,64,65, 50),
            new Background("dark purple hair, ",						100,64,65, 50),
            new Background("and green ",							 25,65,66, 50),
            new Background("and blue ",							 50,65,66, 50),
            new Background("and white ",							 75,65,66, 50),
            new Background("and black ",							100,65,66, 50),
            new Background("ulcerous skin.",						 33,66, 0, 50),
            new Background("scabby skin.",						 66,66, 0, 50),
            new Background("leprous skin.",						100,66, 0, 50)
            #endregion
        };

        public int SocialClass { get; private set; }
        public string History { get; private set; }

        public void Generate(int raceID)
        {

            int hist_ptr = raceID * 3 + 1;
            string history_block = string.Empty;
            int social_class = Dice.RandInt(4);

            bool flag = false;
            while (!flag)
            {
                Background b_ptr = null;

                int test = Dice.RandInt(100);
                var filterdChart = from bg in this.chart
                                   where bg.chart == hist_ptr
                                   select bg;
                b_ptr = filterdChart.FirstOrDefault(bg => bg.roll > test);
                if (b_ptr == null) b_ptr = filterdChart.First();

                history_block += b_ptr.info;
                social_class += b_ptr.bonus - 50;
                hist_ptr = b_ptr.next;
                if (hist_ptr < 1) flag = true;

            }
            this.History = history_block;
            this.SocialClass = social_class < 0 ? 0:social_class;
        }

    }
}
